﻿using AuthCenter.API.Application.Command.Account;
using AuthCenter.Domain;
using AuthCenter.Domain.Account;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using AutoMapper;
using MD5Hash;
using MediatR;
using System.Transactions;
using Yitter.IdGenerator;

namespace AuthCenter.API.Application.Handler.Account
{
    public class UpdateAccountHandler : IRequestHandler<UpdateAccountCommand, APIResult<int>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;
        private readonly IBaseRepository<LogModel> logRepo;
        private readonly IMapper mapper;

        public UpdateAccountHandler(IBaseRepository<AccountModel> accountRepo,
                                    IBaseRepository<LogModel> logRepo,
                                    IMapper mapper)
        {
            this.accountRepo = accountRepo;
            this.logRepo = logRepo;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(UpdateAccountCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = accountRepo.GetAll().FirstOrDefault(x=>x.Id == request.Id);
            if (res == null)
            {
                result.Message = "找不到此编号";
                result.Code = APIEnum.Fail;
                return Task.FromResult(result);
            }
            var user = accountRepo.GetAll().FirstOrDefault(x => x.Name == request.Name&&x.IsDel==false && x.Id != request.Id);
            if (user != null)
            {
                result.Message = "用户名已存在";
                result.Code = APIEnum.Fail;
                return Task.FromResult(result);
            }
            var Model = mapper.Map(request, res);
            Model.PassWord = request.PassWord.GetMD5();
            Model.UpdateName = request.LoginName;
            Model.UpdateDate = DateTime.Now;
            LogModel log = new LogModel()
            {
                Id = YitIdHelper.NextId(),
                LogContent = "用户修改成功",
                CreateDate = DateTime.Now,
                CreateName = request.LoginName
            };
            int r = 0;
            int r1 = 0;
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    r = accountRepo.UpdateAsync(Model).Result;
                    r1 = logRepo.AddAsync(log).Result;
                    tran.Complete();
                }
                catch (Exception)
                {

                    throw;
                }
            }
            if (r > 0)
            {
                result.Message = "用户修改成功";
                result.Code = APIEnum.Ok;
                result.Data = r;
            }
            else
            {
                result.Message = "用户修改失败";
                result.Code = APIEnum.Fail;
                result.Data = r;
            }
            return Task.FromResult(result);
        }
    }
}
